#!/bin/bash
###
 # @Author: admin@attacker.club
 # @Date: 2022-09-29 14:35:55
 # @LastEditTime: 2023-03-06 01:14:56
 # @Description: 
### 


    docker stop zabbix-mysql >2&1
    docker stop zabbix-web >2&1
    docker stop zabbix-server >2&1
    docker rm zabbix-mysql >2&1
    docker rm zabbix-web >2&1
    docker rm zabbix-server >2&1

    ## DB服务
    dataDir="/opt/docker-data/mysql"
    rm ="/opt/docker-data/mysql"
    yum remove mariadb* -y # 卸载默认mariadb
    rpm -ivh https://repo.mysql.com/mysql80-community-release-el7.rpm
    yum install mysql-community-client -y # 安装mysql client


    if [ ! "$rootPassword" ]; then
        rootPassword=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 12`
        zbxPassword=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 12`
        echo "rootPassword=$rootPassword" >> ~/.bashrc;
        echo "zbxPassword=$zbxPassword" >> ~/.bashrc;
    fi 
    
    

    echo "> 启动mysql"
    docker run \
	    --restart always \
	    -d -p 3306:3306 \
	    --name zabbix-mysql \
	    --hostname zabbix-mysql \
	    -e MYSQL_ROOT_PASSWORD=${rootPassword} \
	    -e MYSQL_USER="zabbix" \
	    -e MYSQL_PASSWORD=${zbxPassword} \
	    -e MYSQL_DATABASE="zabbix" \
        -v $dataDir:/var/lib/mysql \
	    mysql:8 --character-set-server=utf8 --collation-server=utf8_bin

    sleep 10
    ## 启动zabbix server
    echo "> 启动zabbix server"
    docker run  -d -p 10051:10051 \
	    --restart always \
	    --name zabbix-server \
	    --hostname zabbix-server \
	    --link zabbix-mysql:mysql \
	    -e DB_SERVER_HOST="mysql" \
	    -e MYSQL_USER="zabbix" \
	    -e MYSQL_PASSWORD="${zbxPassword}" \
	    -v /etc/localtime:/etc/localtime:ro \
	    -v /data/docker/zabbix/alertscripts:/usr/lib/zabbix/alertscripts \
	    -v /data/docker/zabbix/externalscripts:/usr/lib/zabbix/externalscripts \
	    zabbix/zabbix-server-mysql:ubuntu-6.0-latest
    # zabbix/zabbix-server-mysql:latest
   
    
    ## 启动zabbix web 
    echo "> 启动zabbix web"
    docker run  -d -p 81:8080 \
	    --restart always \
	    --name zabbix-web \
	    --hostname zabbix-web \
	    --link zabbix-mysql:mysql \
	    --link zabbix-server:zabbix-server \
	    -e DB_SERVER_HOST="mysql" \
	    -e MYSQL_USER="zabbix" \
	    -e MYSQL_PASSWORD="${zbxPassword}" \
	    -e ZBX_SERVER_HOST="zabbix-server" \
	    -e PHP_TZ="Asia/Shanghai" \
	    zabbix/zabbix-web-nginx-mysql:6.0-alpine-latest
    # zabbix/zabbix-web-nginx-mysql:latest 

    sleep 3
    echo "mysql -h127.0.0.1 -uroot -p$rootPassword" > mysql.txt
    echo "mysql -h127.0.0.1 -uzabbix -p$zbxPassword" >> mysql.txt
    echo "http://zabbix   账号:  Admin / zabbix"
    ## sql添加远程账号
    # CREATE USER  'admin'@'%' ;
    # GRANT ALL ON *.* TO 'admin'@'%' IDENTIFIED WITH mysql_native_password BY 'adminPwd123';